package com.xiehua.task.server.handler;

import com.xiehua.task.intf.AbsBusinessHandler;
import com.xiehua.task.job.TaskReverse;
import com.xiehua.task.job.TaskReverseDefualt;
import com.xiehua.task.job.bucket.DelayBucket;
import com.xiehua.task.job.pool.Job;
import com.xiehua.task.msg.TaskPushRespBody;
import com.xiehua.task.protocol.TaskPacket;
import com.xiehua.task.redis.RedisTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicLong;

import static com.xiehua.task.job.pool.Job.JobStatus.executeed;
import static com.xiehua.task.msg.BaseBody.RESULT_SUCCESS;

/**
 * @author Mr.zhang
 * 2018年3月27日 下午9:51:28
 */
public class TaskPushRespHandler extends AbsBusinessHandler<TaskPushRespBody> {

	private static Logger log = LoggerFactory.getLogger(TaskPushRespHandler.class);

	AtomicLong tokenSeq = new AtomicLong();

	/**
	 *
	 * @author tanyaowu
	 */
	public TaskPushRespHandler() {
	}

	/**
	 * @return
	 * @author tanyaowu
	 */
	@Override
	public Class<TaskPushRespBody> bodyClass() {
		return TaskPushRespBody.class;
	}

	/**
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@Override
	public Object handler(TaskPacket packet, TaskPushRespBody bsBody, ChannelContext channelContext) throws Exception {
		log.info("收到客户端执行task的结果:{}", bsBody.toString());
		TaskReverseDefualt taskReverse = new TaskReverseDefualt(RedisTemplate.getJedis());
		if(RESULT_SUCCESS.equals(bsBody.getResult())){
			taskReverse.executeed(bsBody.getJobId(),channelContext);
		}else{
			taskReverse.fail(bsBody.getJobId());
		}
		taskReverse.clear();
		return null;
	}

	private String newToken() {
		return System.currentTimeMillis() + "_" + tokenSeq.incrementAndGet();
	}
}
